/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import com.elancom.framework.commons.util.MapUtils;
import com.elancom.framework.commons.util.StringUtils;

/**
 * @author elancom
 * @date 2012-7-23
 */
public abstract class ServletUtils {

	public static String getTrimParameter(HttpServletRequest request, String paramName) {
		if (request == null || paramName == null) {
			return null;
		}

		final String value = request.getParameter(paramName);
		return value != null ? value.trim() : value;
	}

	public static String[] getTrimParameterValues(HttpServletRequest request, String paramName) {
		if (StringUtils.isNotBlank(paramName)) {
			final String[] values = request.getParameterValues(paramName);
			if (values != null && values.length > 0) {
				for (int i = 0; i < values.length; i++) {
					values[i] = StringUtils.trim(values[i]);
				}
			}
			return values;
		}
		return null;
	}

	/**
	 * 获取参数数组,长度%2总是为0<br>
	 * 形式如下： [键,值,键,值]
	 * 
	 * @param request
	 * @return
	 */
	public static Object[] getTrimParameterArray(HttpServletRequest request) {
		return getTrimParameterArray(getTrimParameterMap(request));
	}

	/**
	 * @param requestMap
	 * @return
	 */
	public static Object[] getTrimParameterArray(final Map<String, Object> requestMap) {
		if (requestMap == null || requestMap.isEmpty()) {
			return new Object[0];
		}

		int i = 0;
		final Object[] rs = new Object[requestMap.size() * 2];

		for (Entry<String, Object> entry : requestMap.entrySet()) {
			rs[i++] = entry.getKey();
			rs[i++] = entry.getValue();
		}

		return rs;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> getTrimParameterMap(HttpServletRequest request) {
		return getTrimMap((Map<String, Object>) request.getParameterMap());
	}

	/**
	 * @param parameterMap
	 * @return
	 */
	public static Map<String, Object> getTrimMap(Map<String, Object> parameterMap) {
		final Map<String, Object> cloneMap = MapUtils.copy(parameterMap, new HashMap<String, Object>());

		Object value = null;
		String[] values = null;
		String key = null;
		for (Entry<String, Object> entry : parameterMap.entrySet()) {
			value = entry.getValue();
			key = entry.getKey();

			if (value != null && value instanceof Object[]) {
				values = (String[]) value;
				switch (values.length) {
				case 6:
					values[5] = values[5] != null ? values[5].toString().trim() : null;
				case 5:
					values[4] = values[4] != null ? values[4].toString().trim() : null;
				case 4:
					values[3] = values[3] != null ? values[3].toString().trim() : null;
				case 3:
					values[2] = values[2] != null ? values[2].toString().trim() : null;
				case 2:
					values[1] = values[1] != null ? values[1].toString().trim() : null;
				case 1:
					values[0] = values[0] != null ? values[0].toString().trim() : null;
					break;
				default:
					for (int i = 0, lenght = values.length; i < lenght; i++) {
						values[i] = values[i] != null ? values[i].toString().trim() : null;
					}
				}

				// replace
				if (values.length == 1) {
					cloneMap.put(key, values[0]);
				} else {
					cloneMap.put(key, values);
				}
			}
		}

		return cloneMap;
	}

	public static void write(PageContext pageContext, String str) {
		if (pageContext == null || str == null) {
			return;
		}

		try {
			// pageContext.getResponse().getWriter().write(str);
			pageContext.getOut().write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
